package conson.tech.manage.coupons.service.impl;

import conson.tech.core.exception.BaseException;
import conson.tech.core.response.Response;
import conson.tech.manage.common.constant.BusinessMessage;
import conson.tech.manage.coupons.entity.dto.CouponMsgEditDto;
import conson.tech.manage.coupons.entity.dto.OperationLogDto;
import conson.tech.manage.coupons.entity.pojo.Activity;
import conson.tech.manage.coupons.entity.pojo.CouponsBatch;
import conson.tech.manage.coupons.entity.pojo.OperationLog;
import conson.tech.manage.coupons.entity.vo.CouponMessageDetailAVo;
import conson.tech.manage.coupons.entity.vo.CouponMessageDetailCVo;
import conson.tech.manage.coupons.entity.vo.CouponMessageDetailVo;
import conson.tech.manage.coupons.mapper.ActivityMapper;
import conson.tech.manage.coupons.mapper.CouponsBatchMapper;
import conson.tech.manage.coupons.mapper.CouponsMessageCenterMapper;
import conson.tech.manage.coupons.mapper.OperationLogMapper;
import conson.tech.manage.coupons.service.CouponsMessageCenterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * InterfaceName: CouponsMessageCenterServiceImpl
 * Description: 优惠券消息中心业务接口实现类
 * Author: XYF
 * Date: 2020/7/30 21:29
 * Version: 1.0
 */
@Slf4j
@Service("couponsMessageCenterService")
public class CouponsMessageCenterServiceImpl implements CouponsMessageCenterService {

    @Autowired
    private CouponsMessageCenterMapper couponsMessageCenterMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private CouponsBatchMapper couponsBatchMapper;

    @Autowired
    private OperationLogMapper operationLogMapper;

    /**
     * 查看优惠券消息中心详情
     *
     * @param id
     * @param type
     * @return conson.tech.core.response.Response
     */
    @Override
    public Response getCouponMsg1(Long id, Integer type) {
        CouponMessageDetailVo couponMessageDetailVo = new CouponMessageDetailVo();
        if (type == 1) {
            Activity activity = this.activityMapper.selectById(id);
            if (activity != null) {
                couponMessageDetailVo.setActivityId(id);
                couponMessageDetailVo.setArriveType(activity.getArriveType());
                couponMessageDetailVo.setMessageType(activity.getMessageType());
                couponMessageDetailVo.setPushContent(activity.getPushContent());
                couponMessageDetailVo.setPushType(activity.getPushType());
                couponMessageDetailVo.setFlag(type);
                return Response.success(couponMessageDetailVo);
            }
        }
        if (type == 2) {
            CouponsBatch couponsBatch = this.couponsBatchMapper.selectById(id);
            if (couponsBatch != null) {
                couponMessageDetailVo.setBatchId(id);
                couponMessageDetailVo.setExpirationNotice(couponsBatch.getExpirationNotice());
                couponMessageDetailVo.setPushFlag(couponsBatch.getPushFlag());
                couponMessageDetailVo.setSmsDes(couponsBatch.getSmsDes());
                couponMessageDetailVo.setSmsFlag(couponsBatch.getPushFlag());
                couponMessageDetailVo.setFlag(type);
                return Response.success(couponMessageDetailVo);
            }
        }
//        没有相关信息
        throw new BaseException(BusinessMessage.COUPON_MSG_ERROR);
    }

    /**
     * 优惠券消息中心修改
     *
     * @param currentUserId
     * @param currentUsername
     * @param id
     * @param type
     * @param dto
     * @return conson.tech.core.response.Response
     */
    @Override
    public Response updateCouponMsg1(Long currentUserId, String currentUsername, Long id, Integer type, CouponMsgEditDto dto) {

        log.info("优惠券消息中心修改开始");
        if (!(type == 1 || type == 2)) {
            throw new BaseException(BusinessMessage.COUPON_MSG_ARGS_NOT_ILLEGAL_ERROR);
        }
        if (type == 1) {
//            活动的到账提醒修改
            Activity activity = this.activityMapper.selectById(id);
            if (activity == null) {
                throw new BaseException(BusinessMessage.COUPON_MSG_ERROR);
            }
            activity.setMessageType(dto.getMessageType());
            activity.setPushType(dto.getPushType());
            activity.setPushContent(dto.getPushContent());
            activity.setArriveType(dto.getArriveType());

            this.activityMapper.updateById(activity);

            OperationLogDto opera = new OperationLogDto();
            opera.setCreateBy(currentUsername);
            opera.setDomainId(activity.getId());
            opera.setOperateDesc("修改领券活动");
            this.editLog(opera);

            return Response.success(activity.getId());
        }

        if (type == 2) {
//            优惠券的到期提醒修改
            CouponsBatch couponsBatch = this.couponsBatchMapper.selectById(id);
            if (couponsBatch == null) {
                throw new BaseException(BusinessMessage.COUPON_MSG_ERROR);
            }
            couponsBatch.setSmsDes(dto.getSmsDes());
            couponsBatch.setSmsFlag(dto.getSmsFlag());
            couponsBatch.setExpirationNotice(dto.getExpirationNotice());
            couponsBatch.setPushFlag(dto.getPushFlag());

            this.couponsBatchMapper.updateById(couponsBatch);

            OperationLogDto opera = new OperationLogDto();
            opera.setCreateBy(currentUsername);
            opera.setDomainId(couponsBatch.getId());
            opera.setOperateDesc("修改优惠券");
            this.editCouponLog(opera);

            return Response.success(couponsBatch.getId());
        }
        log.info("优惠券消息中心修改完成");

        return Response.fail();
    }

    /**
     * 通用日志写入方法
     * zyf
     * 2020/07/22
     *
     * @param dto 日志入参
     */
    private Response editLog(OperationLogDto dto) {
        try {
            log.info("开始写入日志操作表");
            OperationLog operationLog = new OperationLog();
            operationLog.setType(1);
            operationLog.setCreateBy(dto.getCreateBy());
            operationLog.setCreateTime(LocalDateTime.now());
            operationLog.setDomainId(dto.getDomainId());
            operationLog.setOperateDesc(dto.getOperateDesc());
            operationLog.setOperateType(0);
            operationLog.setRemark(dto.getRemark());
            this.operationLogMapper.insert(operationLog);
            log.info("日志操作表填写完成");
            return Response.success();
        } catch (Exception e) {
            log.error("写入日志操作表失败", e);
            throw new BaseException(BusinessMessage.OPERATION_LOG_WRITE_ERROR);
        }
    }

    /**
     * 通用日志写入方法
     * zyf
     * 2020/07/22
     *
     * @param dto 日志入参
     */
    private Response editCouponLog(OperationLogDto dto) {
        try {
            log.info("开始写入日志操作表");
            OperationLog operationLog = new OperationLog();
            operationLog.setType(0);
            operationLog.setCreateBy(dto.getCreateBy());
            operationLog.setCreateTime(LocalDateTime.now());
            operationLog.setDomainId(dto.getDomainId());
            operationLog.setOperateDesc(dto.getOperateDesc());
            operationLog.setOperateType(1);
            operationLog.setRemark(dto.getRemark());
            this.operationLogMapper.insert(operationLog);
            log.info("日志操作表填写完成");
            return Response.success();
        } catch (Exception e) {
            log.error("写入日志操作表失败", e);
            throw new BaseException(BusinessMessage.OPERATION_LOG_WRITE_ERROR);
        }
    }
}
